#!/bin/sh
#
# Start the Cherokee Web Server.
# http://www.cherokee-project.com/
#
# The following signals are supported by Cherokee:
# SIGHUP 	Restarts the server gracefully
# SIGUSR1 	Restarts the server closing all the opened connections
# SIGUSR2 	Reopens the log files
# SIGTERM 	Exits
#
# Written by Pablo Hernan Saro (pablosaro@gmail.com)

NAME=cherokee
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/lib/cherokee
DAEMON=/usr/sbin/cherokee
PIDFILE=/var/run/cherokee.pid
CONF=/etc/cherokee/cherokee.conf
OPTIONS="-d -C $CONF"

export PATH

test -f $DAEMON || exit 0

case "$1" in
  start)
    echo "Starting $NAME web server..."
    $DAEMON $OPTIONS
    sleep 2
    echo -n "done"
    ;;

  stop)
    echo "Stopping $NAME web server..."
    if [ -f $PIDFILE ]; then
	    PID=`cat $PIDFILE`
            kill -s TERM $PID
	    sleep 4
            echo -n "done"
    else
	    echo -n "Error: $PIDFILE does not exist."
    fi
    ;;

  reload)
    echo "Reloading $NAME configuration"
    if [ -f $PIDFILE ]; then
	    PID=`cat $PIDFILE`
            kill -s HUP $PID
	    sleep 3
            echo -n "done"
    else
	    echo -n "Error: $PIDFILE does not exist."
    fi
    ;;

  rotate-logs)
    echo "Rotating logs"
    if [ -f $PIDFILE ]; then
	    PID=`cat $PIDFILE`
            kill -s USR2 $PID
	    sleep 3
            echo -n "done"
    else
	    echo -n "Error: $PIDFILE does not exist."
    fi
    ;;

  restart)
    echo "Restarting the server closing all the opened connections"
    if [ -f $PIDFILE ]; then
	    PID=`cat $PIDFILE`
            kill -s USR1 $PID
	    sleep 4
            echo -n "done"
    else
	    echo -n "Error: $PIDFILE does not exist."
    fi
    ;;

  force-restart)
    $0 stop
    $0 start
    ;;

  *)
    echo "Usage: $0 {start|stop|reload|rotate-logs|restart|force-restart}"
    exit 1
    ;;
esac

if [ $? == 0 ]; then
	echo .
	exit 0
else
	echo failed
	exit 1
fi
